package com.ljh.account.controller;

import com.ljh.account.dao.Announcement;
import com.ljh.account.service.AnnouncementService;
import com.ljh.account.utils.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/admin")
public class AnnouncementController {

    @Autowired
    private AnnouncementService announcementService;

    // 获取所有公告
    @GetMapping("/announcements")
    public Response getAllAnnouncements(
            @RequestParam int page,
            @RequestParam int size
    ) {
        return Response.success(announcementService.getAllAnnouncements(page,size));
    }

    // 根据ID获取公告
    @GetMapping("/{id}")
    public Announcement getAnnouncementById(@PathVariable Integer id) {
        return announcementService.getAnnouncementById(id);
    }

    // 创建公告
    @PostMapping("/announcements")
    public Response createAnnouncement(@RequestBody Announcement announcement) {
        announcementService.createAnnouncement(announcement);
        return Response.success("公告发布成功");
    }

    // 更新公告
    @PutMapping("/{id}")
    public void updateAnnouncement(@PathVariable Integer id, @RequestBody Announcement announcement) {
        announcement.setAnnouncementId(id);
        announcementService.updateAnnouncement(announcement);
    }

    // 删除公告
    @DeleteMapping("/{id}")
    public void deleteAnnouncement(@PathVariable Integer id) {
        announcementService.deleteAnnouncement(id);
    }

    // 根据日期范围获取公告
    @GetMapping("/filter")
    public List<Announcement> getAnnouncementsByDateRange(@RequestParam("start") Date startDate,
                                                          @RequestParam("end") Date endDate) {
        return announcementService.getAnnouncementsByDateRange(startDate, endDate);
    }
}